package com.jrx.anytxn.param.job.cutover.config;

import com.jrx.anytxn.param.config.AnytxnBatchProperties;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.job.cutover.step.CutoverEndListener;
import com.jrx.anytxn.param.job.cutover.step.CutoverEndProcessor;
import com.jrx.anytxn.param.job.cutover.step.CutoverEndWriter;
import com.jrx.anytxn.param.job.cutover.step.CutoverListener;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 日切结束
 *
 * @author : zhenghaopu
 * 2019/11/18
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class CutoverEndConfiguration {

    @Resource(name = "paramSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;

    @Autowired
    private CutoverEndProcessor cutoverEndProcessor;

    @Autowired
    private CutoverEndWriter cutoverEndWriter;

    @Autowired
    private CutoverEndListener cutoverEndListener;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;


    @Bean
    public Job cutoverEndJob(){
        return jobBuilderFactory.get("cutoverEndJob").incrementer(new RunIdIncrementer()).listener(cutoverEndListener).start(cutoverEndStep()).build();
    }
    @Bean
    public Step cutoverEndStep(){
        return stepBuilderFactory.get("cutoverEndStep")
                .<PrOrganizationTable, PrOrganizationTable>chunk(anytxnBatchProperties.getCutoverConfiguration().getChunkLimit())//表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(cutoverEndReader())
                .processor(cutoverEndProcessor)
                .writer(cutoverEndWriter)
                .transactionManager(platformTransactionManager)

                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getCutoverConfiguration().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<PrOrganizationTable> cutoverEndReader() {

        MyBatisCursorItemReader<PrOrganizationTable> orgCursorItemReader = new MyBatisCursorItemReader<>();
        orgCursorItemReader.setSaveState(false);
        orgCursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        //查询方法
        orgCursorItemReader.setQueryId("com.jrx.anytxn.param.mapper.ext.ExtPrOrganizationTableMapper" +
                ".selectByStatusAndFlag");
        Map<String,Object> map = new HashMap<>();
        //查询参数设置
        map.put("status", Constant.ORG_STATUS_1);
        map.put("batchInProgressFlag", Constant.ORG_BATCH_FLAG_1);
        orgCursorItemReader.setParameterValues(map);
        //同步reader   线程安全
        SynchronizedItemStreamReader<PrOrganizationTable> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(orgCursorItemReader);
        return synchronizedItemStreamReader;
    }
}
